package cn.gok.jwtdemo.interceptor;

import cn.gok.jwtdemo.util.JWTUtil;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: chen fan
 * @since: 2022/11/23 11:31
 * @description:
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 调用请求前，进行过滤
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        //执行操作执行，先进行token的校验
        String token = request.getHeader("token");
        Map<String,Object> result = new HashMap<>();
        response.setContentType("application/json;charset=utf8");


        if (ObjectUtils.isEmpty(token)){
            result.put("code","4000");
            result.put("msg","你还未登录，请登录后重试");
            response.getWriter().println( new ObjectMapper().writeValueAsString(result));
            return false;
        }


        try {
            //校验并解析token
            JWTUtil.verify(token);
        }catch (SignatureVerificationException e){
            log.info(e.getMessage());
            result.put("code","4000");
            result.put("msg","签名不一致，请重新登录");
            response.getWriter().println( new ObjectMapper().writeValueAsString(result));
            return false;
        } catch (TokenExpiredException e){
            log.info(e.getMessage());
            result.put("code","4000");
            result.put("msg","token过期，请重新登录");
            response.getWriter().println( new ObjectMapper().writeValueAsString(result));
            return false;
        }catch (AlgorithmMismatchException e){
            log.info(e.getMessage());
            result.put("code","4000");
            result.put("msg","签名算法不匹配,请重新登录");
            response.getWriter().println( new ObjectMapper().writeValueAsString(result));
            return false;
        }catch (InvalidClaimException e){
            log.info(e.getMessage());
            result.put("code","4000");
            result.put("msg","失效的payload，请重新登录");
            response.getWriter().println( new ObjectMapper().writeValueAsString(result));
            return false;
        }catch (Exception e){
            log.info(e.getMessage());
            result.put("code","4000");
            result.put("msg","token验证失败，请求重新登录后再尝试");
            response.getWriter().println( new ObjectMapper().writeValueAsString(result));
            return false;
        }
        return true;
    }
}
